<?php
class CompensateService extends AbstractService {
    /**
     * @var CompensateService
     */
    private static $instance;

    /**
     * Get the instance of CompensateService.
     *
     * @return CompensateService
     */
    public static function get() {
        if (!self::$instance) {
            self::$instance = new CompensateService();
        }
        return self::$instance;
    }

    /**
     * Construction.
     */
    protected function __construct() {
        parent::__construct();
    }

    /**
     * 处理用户创建时的逻辑
     *
     * @param int $shardId
     * @return void
     */
    public function handleCreate($shardId) {

    }

    /**
     * 处理用户登录时的逻辑
     *
     * @param int $userId
     * @return void
     */
    public function handleLogin($userId) {
        // do nothing
    }

    /**
     * 处理 service 对外提供的全量数据
     *
     * @param int $userId
     * @return array
     */
    public function info($userId) {

    }

    /**
     * @param $userId
     * @return array
     * @throws SzException
     */
    public function getCompensateOrder($userId) {
        $profileInfo = UserService::get()->getInfo($userId, GameConst::USER_INFO_TYPE_COMPENSATE_ORDER);
        $compensate = array();
        if (!$profileInfo) {
            $compensateSettings = SzConfig::get()->loadAppConfig(GameConst::CONFIG_COMPENSATE_ORDER, null, true);
            $platformInfo = UserService::get()->getInfo($userId, GameConst::USER_INFO_TYPE_PLATFORM_INFO);
            if (!$platformInfo) {
                return $compensate;
            }
            $userPlatformId = '';
            foreach ($platformInfo as $platform => $platformId) {
                $userPlatformId = $platformId;
                break;
            }
            if (isset($compensateSettings[$userPlatformId])) {
                $compensate = $compensateSettings[$userPlatformId];
            }

        }
        if (!$compensate && !$profileInfo) {
            /**
             * @var ModuleProfileInfoVoList $profileInfoList
             * @var ModuleProfileInfoVo $platformInfo
             */
            $reqTime = GameService::get()->getReqTime();
            $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
            $profileInfo = new ModuleProfileInfoVo(
                $userId,
                GameConst::USER_INFO_TYPE_COMPENSATE_ORDER,
                $reqTime,
                $reqTime,
                true
            );
            $profileInfoList->addElement($profileInfo);
            $profileInfoList->setElement(GameConst::USER_INFO_TYPE_COMPENSATE_ORDER, $profileInfo);
            SzPersister::get()->setVoList($profileInfoList);
        }

        return $compensate;
    }

    public function getCompensateOne($userId) {
        $profileInfo = UserService::get()->getInfo($userId, GameConst::USER_INFO_TYPE_COMPENSATE_ONE);
        $compensate = array();
        $registerTime = UserService::get()->getUserRegisterTime($userId);
        $compensateTime = strtotime('2018-08-02 23:00:00');
        if (!$profileInfo && $registerTime < $compensateTime) {
            $compensate = array(
                'reward' => array(GameConst::ENTITY_ACTION_GET_MONEY => 60)
            );
        }
        return $compensate;
    }

    /**
     * @param $userId
     * @param $reqTime
     * @return bool|void
     * @throws SzException
     */
    public function compensateOrderReward($userId,$reqTime) {
        /**
         * @var ModuleProfileInfoVoList $profileInfoList
         * @var ModuleProfileInfoVo $platformInfo
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $profileInfo = $profileInfoList->getElement(GameConst::USER_INFO_TYPE_COMPENSATE_ORDER, false);
        if ($profileInfo) {
            throw new SzException(29022);
        }
        $platformInfo = $profileInfoList->getElement(GameConst::USER_INFO_TYPE_PLATFORM_INFO, false);
        if (!$platformInfo) {
            throw new SzException(29023);
        }
        $userPlatformId = '';
        foreach ($platformInfo->getRawInfo() as $platform => $platformId) {
            $userPlatformId = $platformId;
            break;
        }

        $compensateSettings = SzConfig::get()->loadAppConfig(GameConst::CONFIG_COMPENSATE_ORDER, null, true);
        if (!$compensateSettings[$userPlatformId]) {
            throw new SzException(29023);
        }
        $compensate = $compensateSettings[$userPlatformId];

        foreach ($compensate['reward'] as $entityId => $count) {
            GameService::get()->handleEntity($userId, $entityId, $count, true);
        }

        $profileInfo = new ModuleProfileInfoVo(
            $userId,
            GameConst::USER_INFO_TYPE_COMPENSATE_ORDER,
            $reqTime,
            $reqTime,
            true
        );
        $profileInfoList->addElement($profileInfo);
        $profileInfoList->setElement(GameConst::USER_INFO_TYPE_COMPENSATE_ORDER, $profileInfo);
        SzPersister::get()->setVoList($profileInfoList);
        return true;
    }

    /**
     * @param $userId
     * @param $reqTime
     * @return bool
     * @throws SzException
     */
    public function compensateOneReward($userId, $reqTime) {
        /**
         * @var ModuleProfileInfoVoList $profileInfoList
         * @var ModuleProfileInfoVo $profileInfo
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $profileInfo = $profileInfoList->getElement(GameConst::USER_INFO_TYPE_COMPENSATE_ONE, false);
        if ($profileInfo) {
            return true;
        }

        GameService::get()->handleEntity($userId, GameConst::ENTITY_ACTION_GET_MONEY, 60, true);

        $profileInfo = new ModuleProfileInfoVo(
            $userId,
            GameConst::USER_INFO_TYPE_COMPENSATE_ONE,
            $reqTime,
            $reqTime,
            true
        );

        $profileInfoList->addElement($profileInfo);
        $profileInfoList->setElement(GameConst::USER_INFO_TYPE_COMPENSATE_ONE, $profileInfo);
        SzPersister::get()->setVoList($profileInfoList);
        return true;
    }

    /**
     * 第二次订单补偿
     * @param $userId
     * @return int|void
     * @throws SzException
     */
    public function compensateOrder2($userId) {
        /**
         * @var ModuleProfileInfoVoList $profileInfoList
         * @var ModuleProfileInfoVo $profileInfo
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $profileInfo = $profileInfoList->getElement(GameConst::USER_INFO_TYPE_COMPENSATE_ORDER2, false);
        if ($profileInfo) {
            return;
        }
        $reqTime = GameService::get()->getReqTime();
        $profileInfo = new ModuleProfileInfoVo(
            $userId,
            GameConst::USER_INFO_TYPE_COMPENSATE_ORDER2,
            $reqTime,
            $reqTime,
            true
        );
        $profileInfoList->addElement($profileInfo);
        $profileInfoList->setElement(GameConst::USER_INFO_TYPE_COMPENSATE_ONE, $profileInfo);
        SzPersister::get()->setVoList($profileInfoList);

        $compensateSettings = SzConfig::get()->loadAppConfig(GameConst::CONFIG_COMPENSATE_ORDER2, null, true);
        $platformInfo = UserService::get()->getInfo($userId, GameConst::USER_INFO_TYPE_PLATFORM_INFO);
        foreach ($platformInfo as $platform => $platformId) {
            if (empty($compensateSettings[$platformId]) or empty($compensate['reward'])) {
                continue;
            }

            $compensate = $compensateSettings[$platformId];
            foreach ($compensate['reward'] as $entityId => $count) {
                GameService::get()->handleEntity($userId, $entityId, $count, true);
            }
            break;
        }
        return;
    }
}
